#include "mainwidget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QComboBox>
#include <QListView>
#include <QAbstractItemModel>
#include <QStringListModel>
#include <QStringList>

#include <QMessageBox>
#include <QDebug>

const QString MainWidget::m_pInvalidSelection = "Select";
const QString MainWidget::m_pPersThatHaveBorrowedBooks = "Show persons that have borrowed books";
const QString MainWidget::m_pBorrowedBooks = "Show borrowed books";
const QString MainWidget::m_pBooksTimedOut = "Show timeout borrowings";
const QString MainWidget::m_pPersThatHaveBorrowedBooksSometime = "Show persons that have borrowed at least one book";
const QString MainWidget::m_pAllBooks = "Show all books in the library";
const long MainWidget::m_pTimeOutDays = 14;

MainWidget::MainWidget( QWidget *parent )
: QWidget( parent ),
  m_controller( m_bookRepository, m_personRepository )
{
	m_pAddBookButton = new QPushButton( "Add Book" );
	m_pAddPersonButton = new QPushButton( "Add Person" );
	m_pComboBoxOption = new QComboBox;
	m_pHBoxLayout = new QHBoxLayout;
	m_pVBoxLayout = new QVBoxLayout;
	m_pModelList = new QStringList;
	m_pAbstractModel = new QStringListModel( *m_pModelList );
	m_pListView = new QListView;
	m_pListView->setModel( m_pAbstractModel );

	m_pHBoxLayout->addWidget( m_pAddBookButton );
	m_pHBoxLayout->addWidget( m_pAddPersonButton );
	m_pHBoxLayout->addWidget( m_pComboBoxOption );

	m_pVBoxLayout->addLayout( m_pHBoxLayout );
	m_pVBoxLayout->addWidget( m_pListView );

	m_pComboBoxOption->addItem( m_pInvalidSelection );
	m_pComboBoxOption->addItem( m_pPersThatHaveBorrowedBooks );
	m_pComboBoxOption->addItem( m_pBorrowedBooks );
	m_pComboBoxOption->addItem( m_pBooksTimedOut );
	m_pComboBoxOption->addItem( m_pPersThatHaveBorrowedBooksSometime );
	m_pComboBoxOption->addItem( m_pAllBooks );

	connect( m_pComboBoxOption, SIGNAL( currentIndexChanged( int ) ),
	         this, SLOT( updateContents(int) ) );

	connect( m_pAddPersonButton, SIGNAL( clicked() ),
	         &m_personDataCollector, SLOT( show() ) );
	connect( m_pAddBookButton, SIGNAL( clicked() ),
	         &m_bookDataCollector, SLOT( show() ) );

	connect( &m_bookDataCollector, SIGNAL( accepted() ),
	         this, SLOT( addBook() ) );
	connect( &m_personDataCollector, SIGNAL( accepted() ),
	         this, SLOT( addPerson() ) );

	setLayout( m_pVBoxLayout );
}

void MainWidget::updateContents( const int &index )
{
	switch ( index )
	{
	case INVALID_SELECTION: break;
	case PERSONS_THAT_HAVE_BORROWED_BOOKS: showPersThatHaveBorrowedBooks(); break;
	case BORROWED_BOOKS: showBorrowedBooks(); break;
	case BORROWED_BOOKS_TIMED_OUT: showBooksTimedOut(); break;
	case PERSONS_THAT_BORROWED_BOOKS_SOMETIME: showPersThatHaveBorrowedBooksSometime(); break;
	case ALL_BOOKS: showAllBooks(); break;
	default: break;
	}
}

void MainWidget::showPersThatHaveBorrowedBooks()
{
	m_pModelList->clear();
	QVector<Person> registeredPersons = m_controller.getPersonRepository().getList();
	for( int i = 0; i < registeredPersons.size(); i++ )
	{
		if( !registeredPersons.value( i ).getBorrowdBooksList().isEmpty() )
		{
			*m_pModelList << QString::number( registeredPersons.value( i ).getId() ) + " "
			                 + registeredPersons.value( i ).getName() ;
		}
	}

	m_pAbstractModel = new QStringListModel( *m_pModelList );
	m_pListView->setModel( m_pAbstractModel );
}

void MainWidget::showBorrowedBooks()
{
	m_pModelList->clear();
	QVector<Book> registeredBooks = m_controller.getBookRepository().getList();
	for( int i = 0; i < registeredBooks.size(); i++ )
	{
		if( registeredBooks.value( i ).getBorrowDate().isValid() )
		{
			*m_pModelList << QString::number( registeredBooks.value( i ).getId() ) + " "
			                 + registeredBooks.value( i ).getTitle()  + " "
			                 + registeredBooks.value( i ).getAuthor() + " ";
		}
	}

	m_pAbstractModel = new QStringListModel( *m_pModelList );
	m_pListView->setModel( m_pAbstractModel );
}

void MainWidget::showBooksTimedOut()
{
	m_pModelList->clear();
	QVector<Book> registeredBooks = m_controller.getBookRepository().getList();
	for( int i = 0; i < registeredBooks.size(); i++ )
	{
		if( registeredBooks.value( i ).getBorrowDate().isValid() )
			if( registeredBooks.value( i ).getBorrowDate().daysTo( QDate::currentDate() ) > m_pTimeOutDays )
			{
				*m_pModelList << QString::number( registeredBooks.value( i ).getId() ) + " "
				                 + registeredBooks.value( i ).getTitle()  + " "
				                 + registeredBooks.value( i ).getAuthor() + " ";
			}
	}

	m_pAbstractModel = new QStringListModel( *m_pModelList );
	m_pListView->setModel( m_pAbstractModel );
}

void MainWidget::showPersThatHaveBorrowedBooksSometime()
{
	m_pModelList->clear();
	QVector<Book> registeredBooks = m_controller.getBookRepository().getList();
	for( int i = 0; i < registeredBooks.size(); i++ )
	{
		if( registeredBooks.value( i ).wasBorrowed() )
		{
			*m_pModelList << QString::number( registeredBooks.value( i ).getId() ) + " "
			                 + registeredBooks.value( i ).getTitle()  + " "
			                 + registeredBooks.value( i ).getAuthor() + " ";
		}
	}

	m_pAbstractModel = new QStringListModel( *m_pModelList );
	m_pListView->setModel( m_pAbstractModel );
}

void MainWidget::showAllBooks()
{
	m_pModelList->clear();
	QVector<Book> registeredBooks = m_controller.getBookRepository().getList();
	for( int i = 0; i < registeredBooks.size(); i++ )
	{
		*m_pModelList << QString::number( registeredBooks.value( i ).getId() ) + " "
		              + registeredBooks.value( i ).getTitle()  + " "
		              + registeredBooks.value( i ).getAuthor() + " ";
	}

	m_pAbstractModel = new QStringListModel( *m_pModelList );
	m_pListView->setModel( m_pAbstractModel );
}

void MainWidget::addPerson()
{
	m_controller.addPerson( m_personDataCollector.getPerson() );
	m_controller.updateDataBase();
}

void MainWidget::addBook()
{
	m_controller.addBook( m_bookDataCollector.getBook() );
	m_controller.updateDataBase();
 }
